"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _objectSpread3 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _jsxRuntime = require("react/jsx-runtime");

var _antdV = require("antd-v4");

var _classnames = _interopRequireDefault(require("classnames"));

var _react = _interopRequireWildcard(require("react"));

var _Select = require("../Select");

require("./index.less");

var _excluded = ["radioType", "renderFormItem", "mode", "render"];

/**
 * 单选组件
 *
 * @param param0
 * @param ref
 */
var FieldRadio = function FieldRadio(_ref, ref) {
  var radioType = _ref.radioType,
      renderFormItem = _ref.renderFormItem,
      mode = _ref.mode,
      render = _ref.render,
      rest = (0, _objectWithoutProperties2.default)(_ref, _excluded);

  var _useContext = (0, _react.useContext)(_antdV.ConfigProvider.ConfigContext),
      getPrefixCls = _useContext.getPrefixCls;

  var layoutClassName = getPrefixCls('pro-field-radio');

  var _useFieldFetchData = (0, _Select.useFieldFetchData)(rest),
      _useFieldFetchData2 = (0, _slicedToArray2.default)(_useFieldFetchData, 3),
      loading = _useFieldFetchData2[0],
      options = _useFieldFetchData2[1],
      _fetchData = _useFieldFetchData2[2];

  var radioRef = (0, _react.useRef)();
  (0, _react.useImperativeHandle)(ref, function () {
    return (0, _objectSpread3.default)((0, _objectSpread3.default)({}, radioRef.current || {}), {}, {
      fetchData: function fetchData() {
        return _fetchData();
      }
    });
  });

  if (loading) {
    return (0, _jsxRuntime.jsx)(_antdV.Spin, {
      size: "small"
    });
  }

  if (mode === 'read') {
    var optionsValueEnum = (options === null || options === void 0 ? void 0 : options.length) ? options === null || options === void 0 ? void 0 : options.reduce(function (pre, cur) {
      var _cur$value;

      return (0, _objectSpread3.default)((0, _objectSpread3.default)({}, pre), {}, (0, _defineProperty2.default)({}, (_cur$value = cur.value) !== null && _cur$value !== void 0 ? _cur$value : '', cur.label));
    }, {}) : undefined;
    var dom = (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
      children: (0, _Select.proFieldParsingText)(rest.text, (0, _Select.ObjToMap)(rest.valueEnum || optionsValueEnum))
    });

    if (render) {
      return render(rest.text, (0, _objectSpread3.default)({
        mode: mode
      }, rest.fieldProps), dom) || null;
    }

    return dom;
  }

  if (mode === 'edit') {
    var _rest$fieldProps;

    var RadioComponents = radioType === 'button' ? _antdV.Radio.Button : _antdV.Radio;

    var _dom = (0, _jsxRuntime.jsx)(_antdV.Radio.Group, (0, _objectSpread3.default)((0, _objectSpread3.default)({
      ref: radioRef
    }, rest.fieldProps), {}, {
      className: (0, _classnames.default)((_rest$fieldProps = rest.fieldProps) === null || _rest$fieldProps === void 0 ? void 0 : _rest$fieldProps.className, "".concat(layoutClassName, "-").concat(rest.fieldProps.layout || 'horizontal')),
      options: undefined,
      children: options === null || options === void 0 ? void 0 : options.map(function (item) {
        return (0, _jsxRuntime.jsx)(RadioComponents, (0, _objectSpread3.default)((0, _objectSpread3.default)({}, item), {}, {
          children: item.label
        }), item.value);
      })
    }));

    if (renderFormItem) {
      return renderFormItem(rest.text, (0, _objectSpread3.default)({
        mode: mode
      }, rest.fieldProps), _dom) || null;
    }

    return _dom;
  }

  return null;
};

var _default = /*#__PURE__*/_react.default.forwardRef(FieldRadio);

exports.default = _default;